package com.speedchina.doormanager.controller;

import com.speedchina.doormanager.domain.DoorPlateLog;
import com.speedchina.doormanager.dto.DoorPlateTaskDTO;
import com.speedchina.doormanager.dto.TaskDTO;
import com.speedchina.doormanager.enums.TaskTypeEnum;
import com.speedchina.doormanager.service.DoorPlateLogService;
import com.speedchina.doormanager.service.DoorPlateService;
import com.speedchina.doormanager.service.TaskService;
import com.speedchina.doormanager.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/weihu")
public class WeiHuController {

    @Autowired
    private TaskService taskService;

    @Autowired
    private DoorPlateService doorPlateService;


    @Autowired
    private DoorPlateLogService doorPlateLogService;


    @GetMapping
    public String weihuPage(Model model) {
        List<TaskDTO> taskDTOS = taskService.findAllTaskByTaskType(TaskTypeEnum.WEIHU.getStatus());
        model.addAttribute("taskDTOS", taskDTOS);
        return "fenye/weihu";
    }

    /**
     * 门牌安装页面
     *
     * @param taskId
     * @param model
     * @return
     */
    @GetMapping("/list/{taskId}")
    public String weihuTasksPage(@PathVariable("taskId") String taskId, Model model) {
        DoorPlateTaskDTO doorPlateTaskDTO = taskService.findTaskDTOById(taskId);
        model.addAttribute("doorPlateTaskDTO", doorPlateTaskDTO);
        return "fenye/wh-detaillist";
    }

    /**
     * 门牌维护详细页面
     *
     * @param taskId
     * @param doorPlateId
     * @return
     */
    @GetMapping("/list/{taskId}/{doorPlateId}")
    public String getWeihuTaskPageById(@PathVariable("taskId") String taskId,
                                       @PathVariable("doorPlateId") String doorPlateId,
                                       Model model) {
        model.addAttribute("taskId", taskId);
        model.addAttribute("doorPlateId", doorPlateId);
        model.addAttribute("doorPlate", doorPlateService.findById(doorPlateId));
        return "fenye/wh-detail";
    }

    /**
     * 安装门牌
     *
     * @param doorPlateLog
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public ResultVO weihuDoorPlate(DoorPlateLog doorPlateLog) {
        doorPlateLogService.saveDoorPlateLog(doorPlateLog, TaskTypeEnum.WEIHU.getStatus());
        return ResultVO.success();

    }
}
